<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<script src="../dist/iife/spine-webgl.js"></script>
<style>
	html,
	body {
		margin: 0;
		padding: 0;
	}
</style>

<body>
	<canvas id="canvas" style="position: absolute; width: 100%; height: 100%;"></canvas>
</body>
<script>
	// Define the class running in the Spine canvas
	class App {
		skeleton;
		state;

		loadAssets(canvas) {
			canvas.assetManager.AnimationState
			canvas.assetManager.loadTextureAtlas("mix-and-match-pma.atlas");
			canvas.assetManager.loadBinary("mix-and-match-pro.skel");
		}

		initialize(canvas) {
			let assetManager = canvas.assetManager;

			// Create the atlas
			let atlas = canvas.assetManager.require("mix-and-match-pma.atlas");
			let atlasLoader = new spine.AtlasAttachmentLoader(atlas);

			// Create the skeleton
			let skeletonBinary = new spine.SkeletonBinary(atlasLoader);
			skeletonBinary.scale = 0.5;
			let skeletonData = skeletonBinary.readSkeletonData(assetManager.require("mix-and-match-pro.skel"));
			this.skeleton = new spine.Skeleton(skeletonData);

			// Create the animation state
			let stateData = new spine.AnimationStateData(skeletonData);
			this.state = new spine.AnimationState(stateData);
			this.state.setAnimation(0, "dance", true);

			// Create a new skin, by mixing and matching other skins
			// that fit together. Items making up the girl are individual
			// skins. Using the skin API, a new skin is created which is
			// a combination of all these individual item skins.
			let mixAndMatchSkin = new spine.Skin("custom-girl");
			mixAndMatchSkin.addSkin(skeletonData.findSkin("skin-base"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("nose/short"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("eyelids/girly"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("eyes/violet"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("hair/brown"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("clothes/hoodie-orange"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("legs/pants-jeans"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("accessories/bag"));
			mixAndMatchSkin.addSkin(skeletonData.findSkin("accessories/hat-red-yellow"));
			this.skeleton.setSkin(mixAndMatchSkin);
		}

		update(canvas, delta) {
			this.state.update(delta);
			this.state.apply(this.skeleton);
			this.skeleton.updateWorldTransform(spine.Physics.update);
		}

		render(canvas) {
			let renderer = canvas.renderer;
			renderer.resize(spine.ResizeMode.Expand);
			canvas.clear(0.2, 0.2, 0.2, 1);
			renderer.begin();
			renderer.drawSkeleton(this.skeleton, true);
			renderer.end();
		}
	}

	// Create the Spine canvas which runs the app
	new spine.SpineCanvas(document.getElementById("canvas"), {
		pathPrefix: "/assets/",
		app: new App()
	});
</script>

</html>